# 自动测试和报告生成器 - PowerShell版本
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "           自动测试和报告生成器" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host ""

# 设置控制台编码为UTF-8以支持中文显示
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

# 设置路径（从Application.Test.Reporter目录出发）
$PROJECT_ROOT = Join-Path $PSScriptRoot ".."
$TEST_PROJECT = Join-Path $PROJECT_ROOT "Application.Test\Application.Test.csproj"
$TEST_RESULTS_DIR = Join-Path $PROJECT_ROOT "Application.Test\TestResults"
$REPORT_OUTPUT_DIR = Join-Path $PSScriptRoot "Reports"
$REPORTER_PROJECT = Join-Path $PSScriptRoot "Application.Test.Reporter.csproj"

Write-Host "[信息] 项目根目录: $PROJECT_ROOT" -ForegroundColor Gray
Write-Host "[信息] 测试项目: $TEST_PROJECT" -ForegroundColor Gray
Write-Host "[信息] 测试结果目录: $TEST_RESULTS_DIR" -ForegroundColor Gray
Write-Host "[信息] 报告输出目录: $REPORT_OUTPUT_DIR" -ForegroundColor Gray
Write-Host "[信息] 报告生成器项目: $REPORTER_PROJECT" -ForegroundColor Gray
Write-Host ""

# 检查测试项目是否存在
if (-not (Test-Path $TEST_PROJECT)) {
    Write-Host "[错误] 测试项目文件不存在: $TEST_PROJECT" -ForegroundColor Red
    Write-Host "请确保项目结构正确" -ForegroundColor Yellow
    Read-Host "按任意键退出..."
    exit 1
}

# 检查报告生成器项目是否存在
if (-not (Test-Path $REPORTER_PROJECT)) {
    Write-Host "[错误] 报告生成器项目文件不存在: $REPORTER_PROJECT" -ForegroundColor Red
    Write-Host "请确保项目结构正确" -ForegroundColor Yellow
    Read-Host "按任意键退出..."
    exit 1
}

# 清理旧的测试结果
Write-Host "[步骤1] 清理旧的测试结果..." -ForegroundColor Yellow
if (Test-Path $TEST_RESULTS_DIR) {
    Remove-Item -Path $TEST_RESULTS_DIR -Recurse -Force
    Write-Host "[成功] 已清理旧的测试结果" -ForegroundColor Green
} else {
    Write-Host "[信息] 测试结果目录不存在，无需清理" -ForegroundColor Gray
}

# 运行单元测试并生成TRX报告
Write-Host "[步骤2] 运行单元测试并生成TRX报告..." -ForegroundColor Yellow
$testResult = dotnet test $TEST_PROJECT --logger "trx;LogFileName=test-results.trx" --results-directory $TEST_RESULTS_DIR

if ($LASTEXITCODE -ne 0) {
    Write-Host "[警告] 测试执行过程中出现错误，错误代码: $LASTEXITCODE" -ForegroundColor Yellow
    Write-Host "将继续尝试生成报告..." -ForegroundColor Yellow
}

# 检查TRX文件是否生成
Write-Host ""
Write-Host "[步骤3] 检查TRX文件生成情况..." -ForegroundColor Yellow
$trxFiles = Get-ChildItem -Path $TEST_RESULTS_DIR -Filter "*.trx" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending

if ($trxFiles.Count -eq 0) {
    Write-Host "[错误] 未找到TRX文件，无法生成测试报告" -ForegroundColor Red
    Write-Host "请检查测试执行是否成功" -ForegroundColor Yellow
    Read-Host "按任意键退出..."
    exit 1
}

$latestTrxFile = $trxFiles[0].Name
Write-Host "[成功] 找到TRX文件: $TEST_RESULTS_DIR\$latestTrxFile" -ForegroundColor Green

# 构建报告生成器
Write-Host ""
Write-Host "[步骤4] 构建报告生成器..." -ForegroundColor Yellow
Set-Location $PSScriptRoot
$buildResult = dotnet build --nologo

if ($LASTEXITCODE -ne 0) {
    Write-Host "[错误] 报告生成器构建失败" -ForegroundColor Red
    Write-Host "尝试重新构建..." -ForegroundColor Yellow
    $buildResult = dotnet build
    if ($LASTEXITCODE -ne 0) {
        Write-Host "[错误] 报告生成器构建仍然失败" -ForegroundColor Red
        Read-Host "按任意键退出..."
        exit 1
    }
}

Write-Host "[成功] 报告生成器构建完成" -ForegroundColor Green

# 生成测试报告
Write-Host ""
Write-Host "[步骤5] 生成测试报告..." -ForegroundColor Yellow
$runResult = dotnet run --project $REPORTER_PROJECT $TEST_RESULTS_DIR $REPORT_OUTPUT_DIR

if ($LASTEXITCODE -ne 0) {
    Write-Host "[错误] 报告生成失败，错误代码: $LASTEXITCODE" -ForegroundColor Red
    Read-Host "按任意键退出..."
    exit 1
}

# 显示报告文件信息
Write-Host ""
Write-Host "[步骤6] 显示生成的报告文件..." -ForegroundColor Yellow
$reportFiles = Get-ChildItem -Path $REPORT_OUTPUT_DIR -Filter "单元测试报告_*.md" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending

if ($reportFiles.Count -eq 0) {
    Write-Host "[警告] 未找到报告文件" -ForegroundColor Yellow
} else {
    $latestReport = $reportFiles[0]
    Write-Host "[成功] 最新报告文件: $($latestReport.Name)" -ForegroundColor Green
    Write-Host "文件路径: $($latestReport.FullName)" -ForegroundColor Gray
    Write-Host "文件大小: $($latestReport.Length) 字节" -ForegroundColor Gray
    
    # 显示报告前几行内容
    Write-Host ""
    Write-Host "=== 报告摘要 ===" -ForegroundColor Cyan
    Get-Content $latestReport.FullName -TotalCount 10 | ForEach-Object { Write-Host $_ }
}

Write-Host ""
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host "           操作完成" -ForegroundColor Cyan
Write-Host "===============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "测试报告已成功生成并保存在以下目录:" -ForegroundColor Green
Write-Host $REPORT_OUTPUT_DIR -ForegroundColor Gray
Write-Host ""

# 提供打开报告文件的选项
$openReport = Read-Host "是否打开报告文件? (y/n)"
if ($openReport -eq "y" -or $openReport -eq "Y") {
    if ($reportFiles.Count -gt 0) {
        Invoke-Item $latestReport.FullName
        Write-Host "已打开报告文件" -ForegroundColor Green
    } else {
        Write-Host "没有找到可打开的报告文件" -ForegroundColor Yellow
    }
}

Write-Host "脚本执行完成" -ForegroundColor Green